package com.ruoyi.web.controller.work;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.work.domain.WorkExpense;
import com.ruoyi.work.domain.WorkRequestFund;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkInvoice;
import com.ruoyi.work.service.IWorkInvoiceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请开票Controller
 *
 * @author ruoyi
 * @date 2021-12-27
 */
@Controller
@RequestMapping("/work/invoice")
public class WorkInvoiceController extends BaseController {
    private String prefix = "work/invoice";

    @Autowired
    private IWorkInvoiceService workInvoiceService;

    @RequiresPermissions("work:invoice:view")
    @GetMapping()
    public String invoice() {
        return prefix + "/invoice";
    }

    /**
     * 查询申请开票列表
     */
    @RequiresPermissions("work:invoice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkInvoice workInvoice) {
        startPage();
        List<WorkInvoice> list = workInvoiceService.selectWorkInvoiceList(workInvoice);
        return getDataTable(list);
    }

    /**
     * 导出申请开票列表
     */
    @RequiresPermissions("work:invoice:export")
    @Log(title = "导出开票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkInvoice workInvoice) {
        List<WorkInvoice> list = workInvoiceService.selectWorkInvoiceList(workInvoice);
        ExcelUtil<WorkInvoice> util = new ExcelUtil<WorkInvoice>(WorkInvoice.class);
        return util.exportExcel(list, "申请开票数据");
    }

    /**
     * 新增申请开票
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存申请开票
     */
    @RequiresPermissions("work:invoice:add")
    @Log(title = "申请开票", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkInvoice workInvoice) {
        workInvoice.setCreateBy(getLoginName());
        workInvoice.setBelong(getUserName());
        return toAjax(workInvoiceService.insertWorkInvoice(workInvoice));
    }

    /**
     * 修改申请开票
     */
    @RequiresPermissions("work:invoice:edit")
    @GetMapping("/edit/{invoiceId}")
    public String edit(@PathVariable("invoiceId") Long invoiceId, ModelMap mmap) {
        WorkInvoice workInvoice = workInvoiceService.selectWorkInvoiceByInvoiceId(invoiceId);
        mmap.put("workInvoice", workInvoice);
        return prefix + "/edit";
    }

    /**
     * 修改保存申请开票
     */
    @RequiresPermissions("work:invoice:edit")
    @Log(title = "申请开票", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkInvoice workInvoice) {
        workInvoice.setUpdateBy(getLoginName());
        return toAjax(workInvoiceService.updateWorkInvoice(workInvoice));
    }

    /**
     * 删除申请开票
     */
    @RequiresPermissions("work:invoice:remove")
    @Log(title = "申请开票", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(workInvoiceService.deleteWorkInvoiceByInvoiceIds(ids));
    }

    /**
     * 销账查询申请开票列表
     */
    /*@RequiresPermissions("work:invoice:cancel")*/
    @PostMapping("/cancel")
    @ResponseBody
    public TableDataInfo cancel(WorkInvoice workInvoice) {
        startPage();
        workInvoice.setCreateBy(getLoginName());
        List<WorkInvoice> list = workInvoiceService.selectWorkInvoiceList(workInvoice);
        return getDataTable(list);
    }

    /**
     * 查询销账发票
     */
    /*@RequiresPermissions("work:invoice:cancel")*/
    @PostMapping("/cancel/{incomingId}")
    @ResponseBody
    public TableDataInfo cancel(@PathVariable("incomingId") Long incomingId) {
        startPage();
        List<WorkInvoice> list = workInvoiceService.selectInvoiceByCancel(incomingId);
        return getDataTable(list);
    }

    /**
     * 查询销账父子发票、稿件表
     */
    /*@RequiresPermissions("work:invoice:cancel")*/
    @PostMapping("/invoicecancel/{invoiceId}")
    @ResponseBody
    public TableDataInfo invoicecancel(@PathVariable("invoiceId") Long invoiceId) {
        startPage();
        WorkInvoice workInvoice = new WorkInvoice();
        workInvoice.setInvoiceId(invoiceId);
        List<WorkInvoice> list = workInvoiceService.selectInvoicePaperByCancel(workInvoice);
        return getDataTable(list);
    }

    /**
     * 审核发票
     */
    @RequiresPermissions("work:invoice:check")
    @Log(title = "审核发票", businessType = BusinessType.CHECK)
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(String[] ids) {
        try {
            List<WorkInvoice> list = workInvoiceService.selectWorkInvoiceByInvoiceIds(ids);
            if (list.stream().anyMatch(r -> !r.getInvoiceStatus().equals("1"))) {
                return AjaxResult.error("必须为未审核状态");
            }
            list.forEach(e -> {

                e.setInvoiceStatus("2");
                workInvoiceService.updateWorkInvoice(e);
            });
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 开票
     */
    @RequiresPermissions("work:invoice:grant")
    @Log(title = "开票", businessType = BusinessType.GRANT)
    @PostMapping("/grant")
    @ResponseBody
    public AjaxResult grant(String[] ids) {
        try {
            List<WorkInvoice> list = workInvoiceService.selectWorkInvoiceByInvoiceIds(ids);
            if (list.stream().anyMatch(r -> !r.getInvoiceStatus().equals("2"))) {
                return AjaxResult.error("必须为已审核状态");
            }
            list.forEach(e -> {

                e.setInvoiceStatus("3");
                e.setPayTime(DateUtils.getNowDate());
                workInvoiceService.updateWorkInvoice(e);
            });
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
